#include<iostream>
#include<cmath>
#include"ProblemA.h"
using namespace std;

//角度弧度转换
double degree_to_arg(double degree){
    return degree * M_PI / 180;
}

double arg_to_degree(double arg){
    return arg * 180 / M_PI;
}

//print函数将零点用角度单位表示并输出
void print(double arg){
    cout << "alpha的值约为" << arg_to_degree(arg) << endl;
}

double f_a(double alpha){  
    double l = 89, h = 49, D = 55, beta_1 = 11.5 / 180 * M_PI;
    double A = l * sin(beta_1);
    double B = l * cos(beta_1);
    double C = (h + 0.5*D) * sin(beta_1) - 0.5 * D * tan(beta_1);
    double E = (h + 0.5*D) * cos(beta_1) - 0.5 * D; 
    return A * sin(alpha) * cos(alpha) + B * sin(alpha) * sin(alpha) - C * cos(alpha) - E * sin(alpha);
}

double df_a(double alpha){
    double l = 89, h = 49, D = 55, beta_1 = 11.5 / 180 * M_PI;
    double A = l * sin(beta_1);
    double B = l * cos(beta_1);
    double C = (h + 0.5*D) * sin(beta_1) - 0.5 * D * tan(beta_1);
    double E = (h + 0.5*D) * cos(beta_1) - 0.5 * D; 
    return A * cos(2*alpha) + B * sin(2*alpha) + C * sin(alpha) - E * cos(alpha); 
}

double f_b(double alpha){  
    double l = 89, h = 49, D = 30, beta_1 = 11.5 / 180 * M_PI;
    double A = l * sin(beta_1);
    double B = l * cos(beta_1);
    double C = (h + 0.5*D) * sin(beta_1) - 0.5 * D * tan(beta_1);
    double E = (h + 0.5*D) * cos(beta_1) - 0.5 * D; 
    return A * sin(alpha) * cos(alpha) + B * sin(alpha) * sin(alpha) - C * cos(alpha) - E * sin(alpha);
}

double df_b(double alpha){
    double l = 89, h = 49, D = 30, beta_1 = 11.5 / 180 * M_PI;
    double A = l * sin(beta_1);
    double B = l * cos(beta_1);
    double C = (h + 0.5*D) * sin(beta_1) - 0.5 * D * tan(beta_1);
    double E = (h + 0.5*D) * cos(beta_1) - 0.5 * D; 
    return A * cos(2*alpha) + B * sin(2*alpha) + C * sin(alpha) - E * cos(alpha); 
}

int main(){

    double eps = 10e-7;

    Newton Question_a(degree_to_arg(35), eps, f_a, df_a);
    cout << "(a):";
    CheckPrint(Question_a.solve(), f_a);
    print(Question_a.solve());


    Newton Question_b(33 * M_PI / 180, eps, f_b, df_b);
    cout << "(b):";
    CheckPrint(Question_b.solve(), f_b);
    cout << "alpha的值约为" << arg_to_degree(Question_b.solve()) << endl;

    cout << "(c):" << endl;

    cout << "1.选取第一个初始值33, 第二个初始值离33很远" << endl;
    Secant Question_c1(degree_to_arg(33), degree_to_arg(120), eps, f_a);
    CheckPrint(Question_c1.solve(), f_a);
    print(Question_c1.solve());

    cout << "2.选取第一个初始值33, 第二个初始值离33较远,但离此函数另一个零点很近" << endl;
    Secant Question_c2(degree_to_arg(33), degree_to_arg(-11.5), eps, f_a);
    CheckPrint(Question_c2.solve(), f_a);
    print(Question_c2.solve());

    cout << "3.选取两个初始值离33较远" << endl;
    Secant Question_c3(degree_to_arg(60), degree_to_arg(80), eps, f_a);
    CheckPrint(Question_c3.solve(), f_a);
    print(Question_c3.solve());

    cout << "4.选取两个初始值离33很远" << endl;
    Secant Question_c4(degree_to_arg(100), degree_to_arg(120), eps, f_a);
    CheckPrint(Question_c4.solve(), f_a);
    print(Question_c4.solve());

    return 0;
}